//
//  CommonQuestionsCell.m
//  PigMarket
//
//  Created by mac on 2019/4/2.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "CommonQuestionsCell.h"

@interface CommonQuestionsCell ()
/**
 bgView
 */
@property (nonatomic , strong) UIView *bgView;
/**
 topView
 */
@property (nonatomic , strong) UIView *topView;
/**
 topIconImageView
 */
@property (nonatomic , strong) UIImageView *topIconImageView;
/**
 topTitle
 */
@property (nonatomic , strong) UILabel *topTitle;
/**
 灰圆圈
 */
@property (nonatomic , strong) UIView *iconView;
/**
 title
 */
@property (nonatomic , strong) UILabel *title;
/**
 detail
 */
@property (nonatomic , strong) UILabel *detailLab;

@end

@implementation CommonQuestionsCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    CommonQuestionsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[CommonQuestionsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor globalBackgroundColor];
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.topView];
    [self.topView addSubview:self.topIconImageView];
    [self.topView addSubview:self.topTitle];
    [self.bgView addSubview:self.iconView];
    [self.bgView addSubview:self.title];
    [self.bgView addSubview:self.detailLab];
}

- (void)initLayout {
    // 布局子控件
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-1);
    }];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    [self.topIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(15);
    }];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(self.topIconImageView.mas_right).offset(15);
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconView.mas_right).offset(20);
        make.centerY.mas_equalTo(self.iconView.mas_centerY);
        make.right.mas_equalTo(-15);
    }];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.title.mas_bottom).offset(4);
        make.left.mas_equalTo(self.title.mas_left);
        make.bottom.mas_equalTo(-12);
        make.right.mas_equalTo(-15);
    }];
}

#pragma mark - getter and setter
- (void)setQuestionsModel:(CommonQuestions *)questionsModel {
    _questionsModel = questionsModel;
    self.title.text = questionsModel.questionTitle;
    self.detailLab.text = questionsModel.content;
}
- (void)setIsHiddenTopView:(BOOL)isHiddenTopView {
    _isHiddenTopView = isHiddenTopView;
    if (isHiddenTopView) {
        self.topView.hidden = YES;
        [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    } else {
        self.topView.hidden = NO;
        [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(40);
        }];
    }
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 4;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}
- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = [UIColor colorWithHexString:@"#E5F2E4"];
    }
    return _topView;
}
- (UIImageView *)topIconImageView {
    if (!_topIconImageView) {
        _topIconImageView = [[UIImageView alloc] init];
        _topIconImageView.image = [UIImage imageNamed:@"system_questiontitleicon"];
    }
    return _topIconImageView;
}
- (UILabel *)topTitle {
    if (!_topTitle) {
        _topTitle = [[UILabel alloc] init];
        _topTitle.textColor = [UIColor colorWithHexString:@"#262626"];
        _topTitle.font = [UIFont systemFontOfSize:13];
        _topTitle.text = @"热门问题";
    }
    return _topTitle;
}
- (UIView *)iconView {
    if (!_iconView) {
        _iconView = [[UIView alloc] init];
        _iconView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
        _iconView.layer.cornerRadius = 6;
    }
    return _iconView;
}
- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.textColor = [UIColor textBlackColor];
        _title.font = [UIFont middleFont];
    }
    return _title;
}
- (UILabel *)detailLab {
    if (!_detailLab) {
        _detailLab = [[UILabel alloc] init];
        _detailLab.textColor = [UIColor tipsGrayColor];
        _detailLab.font = [UIFont smallFont];
        _detailLab.numberOfLines = 0;
    }
    return _detailLab;
}
@end
